//var rootDocment = 'https://mtt.fengme.cc/tp/brn_mtt/mttadmin/ajax/'; //你的域名  
//var rootDocment = 'http://maott/tp/brn_mtt/mttadmin/ajax/'; //你的域名  
var rootDocment = 'https://maotuotuo.com/tp/brn_mtt/mttadmin/ajax/';

function req(url, data, cb) {
  var uuu = rootDocment + url;
  if(url.indexOf('http')>-1){ uuu=url};
  var session_id = wx.getStorageSync('PHPSESSID');//本地取存储的sessionID
  if (session_id != "" && session_id != null) {
    var header = { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'PHPSESSID=' + session_id }
  } else {
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
  }
  wx.request({
    url: uuu,
    data: data,//Object.assign(data, { time: Date.parse(new Date()) / 1000 }),
    method: 'post',
    header: header,
    success: function (res) {
      if (session_id == "" || session_id == null) {
        wx.setStorageSync('PHPSESSID', res.data.session_id) //如果本地没有就说明第一次请求 把返回的session id 存入本地
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(false)
    }
  })
}

function modal(title, content, cb) {
  wx.showModal({
    title: title,
    content: content,
    success: function (res) {
      // console.log(res)
      if (res.confirm) {
        return typeof cb == "function" && cb(true)
      } else if (res.cancel) {
        return typeof cb == "function" && cb(false)
      }
    }
  })
}

function toast(title, status, second=2,cp) {
  wx.showToast({
    title: title,
    icon: status,
    duration: second*1000
  })
  setTimeout(function () { return typeof cp == "function" && cp() }, second * 1000)
}

function check_member(code, that, cb, cp){ 

    if (!wx.getStorageSync('signature')) {
      wx.redirectTo({
        url: '/pages/home/index'
      });
      return false;
    }

      var userInfo = wx.getStorageSync('userInfo_rawData');
      var signature = wx.getStorageSync('signature');
      wx_login(that, userInfo, signature, code, cb, cp); 
}

function wx_login(that, userinfo, signature, code, cb, cp){
 
  if(wx.getStorageSync('openid')) {
    typeof cb == "function" && cb(userinfo)
    return false;
  }
  req("login.html", { userinfo: userinfo, 'signature': signature, 'code': code }, function (ress) {
    if (ress.status == "success") {
      that.data.openid = ress.userinfo.openid;
      that.globalData.userInfo = ress.userinfo
      that.globalData.userInfo.isread = ress.isread
      wx.setStorageSync('userInfo', ress.userinfo);
      wx.setStorageSync('openid', ress.userinfo.openid );
      wx.setStorageSync("outtime", ress.outtime)
      wx.setStorageSync("3rd_session", ress.mess)
      typeof cb == "function" && cb(that.globalData.userInfo)
    } else if (ress.status == "error") {
      console.log(ress.mess)
      toast(ress.mess)
    } else {
      modal("错误提示", "网络错误，是否重试？", function (re) {
        if(re){
          wx_login(that, userinfo, signature, code, cb, cp)
        }
      })
    }
  })
}

module.exports = {
  req: req,
  modal:modal,
  toast: toast,
  check_member: check_member
}  
